/**
 * 检测渠道是否在线
 */
import React from 'react';
import { useRequest } from '@/hooks';
import { message, Modal } from 'antd';
import { OfflineModal } from '../containers';
import { checkChannelOnline } from '../service';

export default function useChannelOnlineCheck(params) {
    const { run } = useRequest(() => checkChannelOnline(params), { manual: true });

    const check = async () => {
        const { data = [], code, msg = '' } = await run();
        return new Promise(resolve => {
            let isAllOnline = false;
            if (code === 10000) {
                const account = data.reduce((prev, cur) => {
                    if (Array.isArray(cur.accountDetailVOList)) {
                        return [...prev, ...cur.accountDetailVOList];
                    }
                    return [...prev];
                }, []);

                isAllOnline = account.every(item => Number(item.accountStatus) === 1);

                if (!isAllOnline) {
                    Modal.confirm({
                        title: '提示',
                        content: <OfflineModal data={data} />,
                        onOk() {
                            resolve(true);
                        },
                        onCancel() {
                            resolve(false);
                        },
                        okText: '继续',
                        cancelText: '取消',
                        width: 800
                    });
                } else {
                    resolve(true);
                }
            } else {
                resolve(false);
                message.error(msg);
            }
        });
    };

    return { check };
}
